package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText etUsername, etPassword;
    private Button btnLogin;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);

        sharedPreferences = getSharedPreferences("login_info", Context.MODE_PRIVATE);

        // 尝试从 SharedPreferences 中获取保存的用户名和密码
        String savedUsername = sharedPreferences.getString("username", "");
        String savedPassword = sharedPreferences.getString("password", "");

        // 如果有保存的信息，则自动填充到输入框
        if (!savedUsername.isEmpty() && !savedPassword.isEmpty()) {
            etUsername.setText(savedUsername);
            etPassword.setText(savedPassword);
        }

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();

                // 模拟登录验证
                if (!username.isEmpty() && !password.isEmpty()) {
                    // 保存用户名和密码到 SharedPreferences
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("username", username);
                    editor.putString("password", password);
                    editor.apply();

                    // 这里可以添加登录成功后的逻辑，比如跳转到其他界面
                    // 这里简单打印登录成功信息作为示例
                    System.out.println("登录成功！");
                }
            }
        });
    }
}    